//
//  Album.m
//  UI06_UIScrollView_相册
//
//  Created by wyx on 16/3/16.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "Album.h"
#import "MyScorllView.h"

@interface Album () <UIScrollViewDelegate>
//声明两个子控件
@property (nonatomic, retain) UIPageControl *page;
@property (nonatomic, retain) MyScorllView *scroll;


@end

@implementation Album
- (void)dealloc {
    [_page release];
    [_scroll release];
    [super dealloc];
}
#pragma mark - create and init
- (instancetype)initWithFrame:(CGRect)frame withImage:(NSArray<UIImage *> *)images {
    self = [super initWithFrame:frame];
    if (self) {
        [self configWithFrame:frame images:images];
        
    }
    return self;
}
#pragma mark - config
-(void)configWithFrame:(CGRect)frame images:(NSArray *)images {
    //创建 scroll 对象 注意 frame 参数原点不是{0,0}可用 bounds 属性代替
    self.scroll = [[MyScorllView alloc] initWithFrame:self.bounds withImage:images];
    [self addSubview:self.scroll];
    [_scroll release];
    //创建代理人
    self.scroll.delegate = self;
    //设置缩放比例(最大最小)
    self.scroll.minimumZoomScale = 0.5;
    self.scroll.maximumZoomScale = 2;
    
    
    
    
    //创建 pageControll 对象
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 40, frame.size.width, 40)];
    [self addSubview:self.page];
    [_page release];
    //页数
    self.page.numberOfPages = images.count;
    //设置按钮颜色
    self.page.pageIndicatorTintColor = [UIColor yellowColor];
    //添加事件
    [self.page addTarget:self action:@selector(handlePage:) forControlEvents:UIControlEventValueChanged];
}
- (void)handlePage:(UIPageControl *)page {
    //scroll 滚动 (contentOffset)
    [self.scroll setContentOffset:CGPointMake(self.bounds.size.width * page.currentPage, 0) animated:YES];
}
#pragma mark - ScrollViewDelegate协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    //NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    //NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    //NSLog(@"%s", __FUNCTION__);
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    //NSLog(@"%s", __FUNCTION__);

    //page 跟随 scroll滚动而改变
    self.page.currentPage = scrollView.contentOffset.x / self.bounds.size.width;
}
/** 返回需要缩放的视图 */
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return  [scrollView.subviews objectAtIndex:scrollView.contentOffset.x / self.bounds.size.width];
}


@end
